﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports GenDotNet.TemplateSupport
Imports GenDotNet.Task.Common

Public Class MemberInfo

   Private mName As String
   Private mMemberCategory As MemberCategory
   Private mModifiers As MemberModifiers
   Private mScope As Scope
   Private mParameters As ParameterInfoCollection
   Private mReturnType As NetTypeInfo
   Private mDel As MethodDelegate

   Public Sub New( _
            ByVal category As MemberCategory, _
            ByVal name As String, _
            ByVal scope As Scope, _
            ByVal modifiers As MemberModifiers, _
            ByVal returnType As NetTypeInfo, _
            ByVal methodDelegate As MethodDelegate, _
            ByVal ParamArray parameters() As ParameterInfo)
      mName = name
      mMemberCategory = category
      mModifiers = modifiers
      mReturnType = returnType
      mDel = methodDelegate
      mParameters = New ParameterInfoCollection
      For Each param In parameters
         mParameters.Add(param)
      Next
   End Sub

   Public Shared Function NewConstructor( _
            ByVal scope As Scope, _
            ByVal methodDelegate As MethodDelegate, _
            ByVal ParamArray parameters() As ParameterInfo) _
            As MemberInfo
      Return New MemberInfo(MemberCategory.Constructor, Nothing, scope, Nothing, Nothing, methodDelegate, parameters)
   End Function

   Public Shared Function NewConstructor( _
            ByVal scope As Scope, _
            ByVal methodDelegate As MethodDelegate) _
            As MemberInfo
      Return New MemberInfo(MemberCategory.Constructor, Nothing, scope, Nothing, Nothing, methodDelegate, Nothing)
   End Function

   Public Shared Function NewSub( _
            ByVal name As String, _
            ByVal scope As Scope, _
            ByVal modifiers As MemberModifiers, _
            ByVal methodDelegate As MethodDelegate, _
            ByVal ParamArray parameters() As ParameterInfo) _
            As MemberInfo
      Return New MemberInfo(MemberCategory.Sub, name, scope, modifiers, Nothing, methodDelegate, parameters)
   End Function

   Public Shared Function NewSub( _
            ByVal name As String, _
            ByVal scope As Scope, _
            ByVal methodDelegate As MethodDelegate, _
            ByVal ParamArray parameters() As ParameterInfo) _
            As MemberInfo
      Return New MemberInfo(MemberCategory.Sub, name, scope, Nothing, Nothing, methodDelegate, parameters)
   End Function

   Public Shared Function NewSub( _
           ByVal name As String, _
           ByVal scope As Scope, _
           ByVal modifiers As MemberModifiers, _
           ByVal methodDelegate As MethodDelegate) _
           As MemberInfo
      Return New MemberInfo(MemberCategory.Sub, name, scope, modifiers, Nothing, methodDelegate, Nothing)
   End Function

   Public Shared Function NewSub( _
         ByVal name As String, _
         ByVal scope As Scope, _
         ByVal methodDelegate As MethodDelegate) _
         As MemberInfo
      Return New MemberInfo(MemberCategory.Sub, name, scope, Nothing, Nothing, methodDelegate, Nothing)
   End Function

   Public Shared Function NewFunction( _
                ByVal name As String, _
                ByVal scope As Scope, _
                ByVal modifiers As MemberModifiers, _
                ByVal returnType As NetTypeInfo, _
                ByVal methodDelegate As MethodDelegate, _
                ByVal ParamArray parameters() As ParameterInfo) _
                As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, modifiers, returnType, methodDelegate, parameters)
   End Function

   Public Shared Function NewFunction( _
            ByVal name As String, _
            ByVal scope As Scope, _
            ByVal returnType As NetTypeInfo, _
            ByVal methodDelegate As MethodDelegate, _
            ByVal ParamArray parameters() As ParameterInfo) _
            As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, Nothing, returnType, methodDelegate, parameters)
   End Function

   Public Shared Function NewFunction( _
                ByVal name As String, _
                ByVal scope As Scope, _
                ByVal modifiers As MemberModifiers, _
                ByVal returnType As NetTypeInfo, _
                ByVal methodDelegate As MethodDelegate) _
                As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, modifiers, returnType, methodDelegate, Nothing)
   End Function

   Public Shared Function NewFunction( _
                ByVal name As String, _
                ByVal scope As Scope, _
                ByVal returnType As NetTypeInfo, _
                ByVal methodDelegate As MethodDelegate) _
                As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, Nothing, returnType, methodDelegate, Nothing)
   End Function

   Public Shared Function NewProperty( _
                ByVal name As String, _
                ByVal scope As Scope, _
                ByVal modifiers As MemberModifiers, _
                ByVal returnType As NetTypeInfo, _
                ByVal methodDelegate As MethodDelegate, _
                ByVal ParamArray parameters() As ParameterInfo) _
                As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, modifiers, returnType, methodDelegate, parameters)
   End Function

   Public Shared Function NewFunction( _
            ByVal name As String, _
            ByVal scope As Scope, _
            ByVal returnType As NetTypeInfo, _
            ByVal methodDelegate As MethodDelegate, _
            ByVal ParamArray parameters() As ParameterInfo) _
            As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, Nothing, returnType, methodDelegate, parameters)
   End Function

   Public Shared Function NewFunction( _
                ByVal name As String, _
                ByVal scope As Scope, _
                ByVal modifiers As MemberModifiers, _
                ByVal returnType As NetTypeInfo, _
                ByVal methodDelegate As MethodDelegate) _
                As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, modifiers, returnType, methodDelegate, Nothing)
   End Function

   Public Shared Function NewFunction( _
                ByVal name As String, _
                ByVal scope As Scope, _
                ByVal returnType As NetTypeInfo, _
                ByVal methodDelegate As MethodDelegate) _
                As MemberInfo
      Return New MemberInfo(MemberCategory.Function, name, scope, Nothing, returnType, methodDelegate, Nothing)
   End Function



   Public Property Name() As String
      Get
         Return mName
      End Get
      Set(ByVal value As String)
         mName = value
      End Set
   End Property

   Public Property MemberCategory() As MemberCategory
      Get
         Return mMemberCategory
      End Get
      Set(ByVal value As MemberCategory)
         mMemberCategory = value
      End Set
   End Property


   Public Property Scope() As Scope
      Get
         Return mScope
      End Get
      Set(ByVal value As Scope)
         mScope = value
      End Set
   End Property

   Public Property Modifiers() As MemberModifiers
      Get
         Return mModifiers
      End Get
      Set(ByVal value As MemberModifiers)
         mModifiers = value
      End Set
   End Property

   Public Property Parameters() As ParameterInfoCollection
      Get
         Return mParameters
      End Get
      Set(ByVal value As ParameterInfoCollection)
         mParameters = value
      End Set
   End Property

   Public Property ReturnType() As NetTypeInfo
      Get
         Return mReturnType
      End Get
      Set(ByVal value As NetTypeInfo)
         mReturnType = value
      End Set
   End Property

   Public Property MethodDelegate() As MethodDelegate
      Get
         Return mDel
      End Get
      Set(ByVal value As MethodDelegate)
         mDel = value
      End Set
   End Property

End Class

Namespace VB
   Public Module MemberInfoExtensions
      <Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal info As MemberInfo) As String
         Select Case info.MemberCategory
            Case MemberCategory.Constructor
               Return <code>
   Protected Sub New(<%= info.Parameters.Output %>)
   <%= info.MethodDelegate.Invoke() %>
   End Function
</code>.Value
            Case MemberCategory.Sub
               Return <code>
   Protected Sub <%= info.Name %>(<%= info.Parameters.Output %>)
   <%= info.MethodDelegate.Invoke %>
   End Function
</code>.Value
            Case MemberCategory.Function
               Return <code>
   Protected Function <%= info.Name %>(<%= info.Parameters.Output %>) As <%= info.ReturnType.Output %><%= TemplateBase.crlf %><%= Spaces(6) %>
                         <%= info.MethodDelegate.Invoke() %>
   End Function
</code>.Value
         End Select
      End Function

   End Module
End Namespace

Namespace CSharp
   Public Module MemberInfoExtensions

   End Module
End Namespace
